package com.senken.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.senken.mapper.ApproveMapper;
import com.senken.entity.Approve;
import com.senken.entity.ApproveVo;
import com.senken.entity.SkUser;
import com.senken.service.IApproveService;
import com.senken.service.ISkUserService;
import com.baomidou.framework.service.impl.SuperServiceImpl;

/**
 *
 * Approve 表数据服务层接口实现类
 *
 */
@Service
public class ApproveServiceImpl extends SuperServiceImpl<ApproveMapper, Approve> implements IApproveService {

	@Autowired
	private ApproveMapper approveMapper;
	@Autowired
	private ISkUserService userservice;

	@Override
	public ApproveVo selectApproveBySid(int hid, int sid){
		Approve approve = new Approve();
		approve.setHid(hid);
		approve.setSid(sid);
		return approveMapper.selectApprove(approve);
	}
	
	@Override
	public List<ApproveVo> approves(int hid){
		List<ApproveVo> list = new ArrayList<ApproveVo>();
		for (int i = 1; i <=3; i++) {
			ApproveVo approve = this.selectApproveBySid(hid, i);
			if(approve.getOpinion()==null){
				approve.setTime(new Date());
				list.add(approve);
				break;
			}
			list.add(approve);
		}
		return list;
	}
	
	@Override
	public boolean updateApprove(String device,int opinion,int hid,int sid){
		Approve approve = new Approve();
		approve.setHid(hid);
		approve.setSid(sid);
		approve = this.selectOne(approve);
		approve.setDevice(device);
		approve.setOpinion(opinion);
		approve.setTime(new Date());
		return this.updateById(approve);
	}
	@Override
	public boolean add(int uid,int sid,int hid){
		Approve approve = new Approve();
		approve.setHid(hid);
		approve.setSid(sid);
		approve.setUid(uid);
		approve.setFid(4);
		return insert(approve);
	}
	
	@Override
	public boolean addapprove(int hid,int roleid){
		boolean info = true;
		for (int sid= 1; sid <=3; sid++) {
			if(sid==1){
				SkUser user = userservice.findOne(6);
				this.add(user.getUserId(), sid, hid);
			}
			if(sid==2){
				SkUser user = userservice.findOne(4);
				this.add(user.getUserId(), sid, hid);
			}
			if(sid==3){
				SkUser user = userservice.findOne(roleid);
				this.add(user.getUserId(), sid, hid);
			}
		}
		return info;
	}
	
	
}